package com.chainofresponsibility;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        Request request = new Request();
        request.str = "<>996 kepler";
        Response response = new Response();
        response.str = "response";
        FilterChain filterChain = new FilterChain();
        filterChain.add(new HTMLFilter()).add(new SensitiveFilter()).add(new OtherFilter());
        filterChain.doFilter(request,response);
        System.out.println(request.str);
        System.out.println(response.str);
    }
}

class FilterChain {
    List<Filter> filters = new ArrayList<>();
    int index = 0;
    public FilterChain add(Filter f){
        filters.add(f);
        return this;
    }

    public boolean doFilter(Request request, Response response){
        if(index == filters.size()) return false;
        Filter f = filters.get(index);
        index ++;
        return f.doFilter(request, response, this);
    }
}

interface Filter{
    boolean doFilter(Request request, Response response, FilterChain filterChain);
}

class HTMLFilter implements Filter{

    @Override
    public boolean doFilter(Request request, Response response, FilterChain filterChain) {
        request.str = request.str.replace('<', '[')
                .replace('>', ']') + " HTMLFilter";
        filterChain.doFilter(request, response);
        response.str += " HTMLFilter";
        return true;
    }
}

class SensitiveFilter implements Filter{

    @Override
    public boolean doFilter(Request request, Response response, FilterChain filterChain) {
        request.str = request.str.replaceAll("996", "995") + " SensitiveFilter";
        filterChain.doFilter(request, response);
        response.str += " SensitiveFilter";
        return true;
    }
}

class OtherFilter implements Filter{

    @Override
    public boolean doFilter(Request request, Response response, FilterChain filterChain) {
        request.str = request.str.replaceAll("kepler", "wsk") + " OtherFilter";
        filterChain.doFilter(request, response);
        response.str += " OtherFilter";
        return true;
    }
}

class Request{
    String str;
}

class Response{
    String str;
}
